package com.zzptc.easyshop.servlet;

import com.zzptc.easyshop.dao.IDAO;
import com.zzptc.easyshop.dao.UserDao;
import com.zzptc.easyshop.vo.Users;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.xml.crypto.Data;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.logging.SimpleFormatter;

@WebServlet(name = "MyLoginServlet")
public class MyLoginServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Date date = new Date( );
        SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd");
        String time=simpleDateFormat.format(date.getTime());
        System.out.println("ssss"+time);


        String uname=request.getParameter( "userName" );
        String pwd =request.getParameter( "passWord" );
        int role=Integer.parseInt( request.getParameter( "role" ) );
        IDAO dao = new UserDao();
        Users user = new Users();
        user.setEu_user_id( uname );
        user.setEu_password( pwd );
        user.setEu_status(role);
        HttpSession session = request.getSession();
        boolean flag =((UserDao)dao).login( user );
        if(flag){
            if (role==1){
                session.setAttribute( "role",role);//记录
                session.setAttribute( "username",uname);//记住用户名字
                session.setAttribute( "time",time);//记住服务器的时间
                session.setAttribute( "loginerror","");//清楚登陆失败

                request.getRequestDispatcher( "productServlet" ).forward( request,response );
            }
            if (role==2){
                request.getRequestDispatcher( "usermangerServlet").forward( request,response );
            }else{
                session.setAttribute( "loginerror","失败");
                request.getRequestDispatcher( "erroe.jsp" ).forward( request,response );
            }
        }
    }
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    doPost( request,response );
    }
}
